package com.example.trans;

import com.example.trans.pojo.Account;
import com.example.trans.service.AccountService;
import com.example.trans.service.impl.IsolationService1;
import com.example.trans.service.impl.IsolationService2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * ClassName:testTrans
 * Package:com.example.trans
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/5/11-11:03
 * @Version:v1.0
 */
public class testTrans {

    //全注解开发
    @Test
    public void test() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);

        accountService.transfer("act-001", "act-002", 10000);
        System.out.println("转账成功");
    }

    @Test
    public void testIsolation1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bank.xml");
        IsolationService1 i1 = applicationContext.getBean("i1", IsolationService1.class);
        i1.getByActno("act-004");
    }

    @Test
    public void testIsolation2() throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bank.xml");
        IsolationService2 i2 = applicationContext.getBean("i2", IsolationService2.class);
        Account account = new Account("act-004", 1000.0);
        i2.save(account);
    }

    @Test
    public void name() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bank.xml");
        //获取一号service
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        Account act = new Account("act-003", 1000.0);
        accountService.save(act);

    }

    @Test
    public void testTrans() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bank.xml");
        AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
        try {
            accountService.transfer("act-001", "act-002", 10000);
            System.out.println("转账成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
